package com.chenyue.cm.live.service.impl;


import com.chenyue.cm.base.dao.LcBaseMapper;
import com.chenyue.cm.base.service.BaseServiceImpl;
import com.chenyue.cm.live.dao.JdLiveApplyMapper;
import com.chenyue.cm.live.domain.JdLiveApply;
import com.chenyue.cm.live.service.JdLiveApplyService;
import com.chenyue.cm.utils.DateUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;


/**
 * @author chenyue-cm
 */
@Service(value = "JdLiveApplyService")
public class JdLiveApplyServiceImpl extends BaseServiceImpl<JdLiveApply> implements JdLiveApplyService {

    private final JdLiveApplyMapper jdLiveApplyMapper;

    public JdLiveApplyServiceImpl(@Qualifier("jdLiveApplyMapper") JdLiveApplyMapper jdLiveApplyMapper) {
        this.jdLiveApplyMapper = jdLiveApplyMapper;
    }

    @Override
    @Resource(name = "jdLiveApplyMapper")
    public void setBaseMapper(LcBaseMapper<JdLiveApply, Serializable> baseMapper) {
        this.baseMapper = baseMapper;
    }

    @Override
    public String applyLive(Integer userId) {
        JdLiveApply liveApply = lambdaQuery()
                .eq(JdLiveApply::getUserId, userId)
                .last(" limit 1")
                .one();
        if (null != liveApply) {
            if (liveApply.getState() == 2) {
                liveApply.setState(0);
                jdLiveApplyMapper.updateById(liveApply);
                return "";
            }
            return "该用户已提交申请";
        } else {
            //添加申请记录
            JdLiveApply apply = new JdLiveApply();
            apply.setUserId(userId);
            apply.setState(0);
            apply.setCt(DateUtils.getToday());
            jdLiveApplyMapper.insert(apply);
            return "申请成功";
        }
    }
}
